/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.Disposition;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DelegateAuth
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   private static final long DEFAULT_AUTH_LIFETIME = 3600L;
/*     */   public static final String BY_NAME = "name";
/*     */   public static final String BY_ID = "id";
/*     */   
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  53 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException {
/*  57 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  59 */     Element a = request.getElement("account");
/*  60 */     String key = a.getAttribute("by");
/*  61 */     String value = a.getText();
/*     */     
/*  63 */     long lifetime = request.getAttributeLong("duration", 3600L) * 1000L;
/*     */     
/*  65 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  67 */     Account account = null;
/*     */     
/*  69 */     if (key.equals("name")) {
/*  70 */       account = prov.get(Key.AccountBy.name, value, zsc.getAuthToken());
/*  71 */     } else if (key.equals("id")) {
/*  72 */       account = prov.get(Key.AccountBy.id, value, zsc.getAuthToken());
/*     */     } else {
/*  74 */       throw ServiceException.INVALID_REQUEST("unknown value for by: " + key, null);
/*     */     }
/*     */     
/*  77 */     if (account == null) {
/*  78 */       throw AccountServiceException.NO_SUCH_ACCOUNT(value);
/*     */     }
/*  80 */     checkAdminLoginAsRight(zsc, prov, account);
/*     */     
/*  82 */     ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "DelegateAuth", "accountId", account.getId(), "accountName", account.getName() }));
/*     */     
/*     */ 
/*  85 */     Element response = zsc.createElement(AdminConstants.DELEGATE_AUTH_RESPONSE);
/*  86 */     long maxLifetime = account.getTimeInterval("zimbraAuthTokenLifetime", 3600000L);
/*     */     
/*     */ 
/*  89 */     long expires = System.currentTimeMillis() + Math.min(lifetime, maxLifetime);
/*     */     
/*  91 */     Account adminAcct = prov.get(Key.AccountBy.id, zsc.getAuthtokenAccountId(), zsc.getAuthToken());
/*  92 */     if (adminAcct == null) {
/*  93 */       throw AccountServiceException.NO_SUCH_ACCOUNT(zsc.getAuthtokenAccountId());
/*     */     }
/*  95 */     AuthToken at = AuthProvider.getAuthToken(account, expires, false, adminAcct);
/*  96 */     at.encodeAuthResp(response, true);
/*  97 */     response.addAttribute("lifetime", lifetime, Element.Disposition.CONTENT);
/*  98 */     return response;
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 103 */     relatedRights.add(Rights.Admin.R_adminLoginAs);
/* 104 */     relatedRights.add(Rights.Admin.R_adminLoginCalendarResourceAs);
/* 105 */     notes.add("If the target is an account, need the adminLoginAs right; If the target is a calendar resource, need the R_adminLoginCalendarResourceAs right.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/DelegateAuth.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */